package com.cortex.carambola.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.VideoView;

import com.cortex.carambola.fetch.FetchVideos.Video;
import com.cortex.carambolasummary.R;
import com.google.android.youtube.player.YouTubeIntents;

public class ShowMediaView extends RelativeLayout implements ShowMedia{

	private WebView wbFlshVideo;
	private VideoView vvFile;
	private ImageView hide;
	private int durationAnimation = 1000;

	public ShowMediaView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public ShowMediaView(Context context) {
		super(context);
	}
	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		super.onFinishInflate();
		init();
		initHanlers();
	}
	private void init(){
		hide = (ImageView) findViewById(R.id.imgHide);
		wbFlshVideo = (WebView) findViewById(R.id.wbVideoFlash);
		wbFlshVideo.getSettings().setLoadsImagesAutomatically(true);
		wbFlshVideo.getSettings().setJavaScriptEnabled(true);
		wbFlshVideo.getSettings().setUseWideViewPort(true);
		wbFlshVideo.getSettings().setLoadWithOverviewMode(true);
		wbFlshVideo.getSettings().setPluginState(PluginState.ON);
		wbFlshVideo.setWebViewClient(new WebViewClient());

		// init videoView file
		vvFile = (VideoView)findViewById(R.id.vvFile);
		MediaController mc = new MediaController(getContext());
		mc.setAnchorView((View) vvFile.getParent());
		vvFile.setMediaController(mc);
	}
	private void initHanlers(){
		wbFlshVideo.setOnTouchListener(new OnTouchListener() {
			private boolean isPlaying = false;
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_UP){
					if(isPlaying){
						wbFlshVideo.onPause();
					}
					else{
						wbFlshVideo.onResume();
					}
					isPlaying = !isPlaying;
				}
				return true;
			}
		});
		// to allow html5 videos in webView 
		wbFlshVideo.setWebChromeClient(new WebChromeClient() {
		});
		hide.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_UP){
					vvFile.pause();
					// to stop video flash load empty url
					wbFlshVideo.onPause();    
					wbFlshVideo.loadUrl("");
					ShowMediaView.this.animate()
					.alpha(0f)
					.setDuration(durationAnimation)
					.setListener(new AnimatorListenerAdapter() {
						@Override
						public void onAnimationEnd(Animator animation) {
							setVisibility(View.GONE);
						}
					});
				}
				return true;
			}
		});
	}
	@Override
	public void showVideo(Video video) {
		vvFile.pause();
		wbFlshVideo.onPause();
		wbFlshVideo.setVisibility(View.GONE);
		vvFile.setVisibility(View.GONE);
		switch(video.getVideoType()){
		case FILE:
			vvFile.setVideoPath(video.getUrl());
			((ViewGroup)vvFile.getParent()).setVisibility(View.VISIBLE);
			vvFile.setVisibility(View.VISIBLE);
			vvFile.start();
			show();
			break;
		case FLASH:
			wbFlshVideo.loadUrl(video.getUrl());
			wbFlshVideo.setVisibility(View.VISIBLE);
			show();
			break;
		case YOUTUBE:
			// get the key of video from url 
			int startKey = video.getUrl().indexOf("=");
			String youtubeKeyVideo = video.getUrl().substring(startKey+1);
			// start 
			if(!YouTubeIntents.isYouTubeInstalled(getContext())){
				String message = getResources().getString(R.string.youtubeNotInstalled);
				Toast.makeText(getContext(), message, Toast.LENGTH_LONG).show();
				return;
			}
			Intent intent = YouTubeIntents.createPlayVideoIntentWithOptions(getContext(), youtubeKeyVideo, true, false);
			getContext().startActivity(intent);
			break;
		default:
			break;

		}

	}
	private void show(){
		setAlpha(0f);
		setVisibility(View.VISIBLE);
		animate()
		.alpha(1f)
		.setDuration(durationAnimation)
		.setListener(null);
	}
	@Override
	public void showImage(Drawable image) {
		// TODO Auto-generated method stub

	}
}
